package com.example.gyt.util;

public class ResultUtil {
    //是否成功
    private Boolean success;
    //状态码
    private Integer code;
    //提示信息
    private String msg;
    //数据
    private Object data;


    public ResultUtil(){

    }

    //自定义返回结果的构造方法
    public ResultUtil(Boolean success, Integer code, String msg, Object data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Boolean getSuccess() {
        return success;
    }
    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }



    public enum ResultCode{
        // 数据操作错误定义
        SUCCESS(200, "操作成功!"),
        NO_PERMISSION(403,"您当前没有权限!"),
        NO_AUTH(401,"当前用户未登录!"),
        NOT_FOUND(404, "对不起，服务器未找到该资源!"),
        INTERNAL_SERVER_ERROR(500, "服务器异常，请联系管理员!"),
        PARAMETER_ERROR(502, "您输入的参数有误，请重新再试!"),
        ;

        /** 错误码 */
        private Integer code;

        /** 错误信息 */
        private String msg;

        ResultCode(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public Integer getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }
    }



    //自定义成功返回的结果，无数据
    public static ResultUtil success(){
        ResultUtil result = new ResultUtil();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        return result;
    }


    //自定义成功返回的结果，有数据返回
    public static ResultUtil success(Object data){
        ResultUtil result = new ResultUtil();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }


    //自定义成功时的数据返回接口
    public static ResultUtil success(Boolean success, Integer code, String message, Object succeed) {
        ResultUtil result = new ResultUtil();
        result.setSuccess(success);
        result.setCode(code);
        result.setMsg(message);
        result.setData(succeed);
        return result;
    }


    //失败时的返回接口，无数据返回
    public static ResultUtil failure(ResultCode resultCode){
        ResultUtil result = new ResultUtil();
        result.setSuccess(false);
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMsg());
        return result;
    }

    //失败时的返回接口，有数据返回
    public static ResultUtil failure(ResultCode resultCode, Object data){
        ResultUtil result = new ResultUtil();
        result.setSuccess(false);
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMsg());
        result.setData(data);
        return result;
    }

    //失败时的返回接口，有数据返回
    public static ResultUtil failure(Object data){
        ResultUtil result = new ResultUtil();
        result.setSuccess(false);
        result.setCode(ResultCode.PARAMETER_ERROR.getCode());
        result.setMsg(ResultCode.PARAMETER_ERROR.getMsg());
        result.setData(data);
        return result;
    }

    //自定义失败时的数据返回接口
    public static ResultUtil failure(Boolean success, Integer code, String message, Object errors) {
        ResultUtil result = new ResultUtil();
        result.setSuccess(success);
        result.setCode(code);
        result.setMsg(message);
        result.setData(errors);
        return result;
    }


}
